<?php
require_once('bootstrap.php');   
class request{
  var $opt;
  var $sid;
  var $num_alt;
  var $config_id;
  var $incl_arl;
  var $exc_arl;
  var $ptc;
  var $arl_opt;
  var $id;
  var $status;
     function request($ptc_arr,$opt_arr,$incl_arr,$exc_arr,$alt,$sid,$config_id,$arl_opt,$id=null,$status)
     {
          $this->opt=implode(",",$opt_arr);
          if(count($incl_arr>0)&& $incl_arr!='')
          {
			  
          $this->incl_arl=implode(",",$incl_arr);
          }
		  else
          {
              $this->incl_arl=null ;
          }
          if(count($exc_arr)>0 && $exc_arr!='')
          {
          $this->exc_arl=implode(",",$exc_arr);
          }
          else
          {
              $this->exc_arl=null;
          }
          $this->ptc=implode(",",$ptc_arr);
          $this->config_id=$config_id;
          $this->num_alt=$alt;
          $this->sid=$sid;
          $this->arl_opt=$arl_opt;
		  $this->status=$status;
          if($id==null){
          $this->persist($this);
          }
          else
          {$this->id=$id;
              $this->update($this);
          }
      }
      private function update($ele)
      {
          $pst= new persister();
          $pst->update_request($ele);
      }
      private function persist($ele)
      {
          $pst= new persister();
          $this->id=$pst->persist_request($ele);
      }
  }
class config{
      var $dep_code;
      var $arr_code;
      var $dep_type;
      var $arr_type;
      var $priority;
      var $status;
      var $con;
      var $id;
      var $update;
      public function config($depc,$arrc,$dept,$arrt,$st,$id=null)
      {
          $this->dep_code=$depc;
          $this->dep_type=$dept;
          $this->arr_type=$arrt;
          $this->arr_code=$arrc;
          $this->compute_priority($arrt,$dept);
          if($st==true)
          {
          $this->status=1;
          }
          else
          {
              $this->status=0;
          }
          if($id==null)
          {
          $this->id=strtoupper(substr(uniqid("CN"),0,25));
          }
          else
          {
              $this->id=$id;
              $this->update=true;
          }
          
      }
      
      public function persist()
      {
      $p= new persister() ;
      if($this->update==true){
       return $p->update_config($this);
      }
      else
      {
      return $p->persist_config($this); 
      }
      }
      
      private function compute_priority($at,$dt){
           if($this->dep_type==1&&$this->arr_type==1)
           {
               $this->priority=0;
           }else if($this->dep_type==1&&$this->arr_type==2)
           {
               $this->priority=1;
           }else if($this->dep_type==1&&$this->arr_type==3)
           {
               $this->priority=3;
           }else if($this->dep_type==1&&$this->arr_type==4)
           {
               $this->priority=5;
           }else if($this->dep_type==2&&$this->arr_type==1)
           {
               $this->priority=2;
           }else if($this->dep_type==2&&$this->arr_type==2)
           {
               $this->priority=7;
           }else if($this->dep_type==2&&$this->arr_type==3)
           {
               $this->priority=8;
           }else if($this->dep_type==2&&$this->arr_type==4)
           {
               $this->priority=10;
           }else if($this->dep_type==3&&$this->arr_type==1)
           {
               $this->priority=4;
           }else if($this->dep_type==3&&$this->arr_type==2)
           {
               $this->priority=9;
           }else if($this->dep_type==3&&$this->arr_type==3)
           {
               $this->priority=12;
           }else if($this->dep_type==3&&$this->arr_type==4)
           {
               $this->priority=13;
           }else if($this->dep_type==4&&$this->arr_type==1)
           {
               $this->priority=6;
           }else if($this->dep_type==4&&$this->arr_type==2)
           {
               $this->priority=11;
           }else if($this->dep_type==4&&$this->arr_type==3)
           {
               $this->priority=14;
           }else
           if($this->dep_type==4&&$this->arr_type==4)
           {
               $this->priority=15;
           }
           else
           {$this->priority=100;}
           
      }
  }
class persister{
      var $con;
      function persister()
      {
       
      
      }
      
      function delete_request($request_id)
      {
          try{
          $req= Doctrine::getTable("Requests")->find($request_id);
          $req->delete();
          return true;
          }
          catch(Exception $e)
          {
              return false;
          }
      }
      function update_config($config)
      {
        $con=Doctrine::getTable('Configuration')->find($config->id);
        $con->__set('dep_code',$config->dep_code);
        $con->__set('dep_type',$config->dep_type);
        $con->__set('arr_code',$config->arr_code);
        $con->__set('timestamp',date("y-m-d h:m:s"));
        $con->__set('arr_type',$config->arr_type);
        $con->__set('priority',$config->priority);
        $con->__set('status',$config->status);
        $con->__set('configuration_name',strtoupper($config->dep_code.$config->dep_type."_".$config->arr_code.$config->arr_type."_CN"));
        try{
            $con->save();
            return true;
        }
        catch(Exception $e){
            return false;
        }
             
          
      }
      function delete_config($config_id)
      {
       $con=Doctrine::getTable('Configuration')->find($config_id);
       $qr=new Doctrine_Query() ;
       $qr->select("a.*")->from("MarkupConfigs a")->where("a.config_id='$config_id'");
       $result=$qr->execute();
       $result->toArray();
       foreach($result as $ele)
       {
        $rule=Doctrine::getTable("MarkupRules")->find($ele["rule_id"]);
        $rule->delete();
        $mark_con=Doctrine::getTable("MarkupConfigs")->find($ele["idMarkup_configs"]);
        $mark_con->delete();
       }
       $con->delete();
       
      }
      function persist_config($config)
      {
        $con= new Configuration();
        $con->__set('idConfiguration',$config->id);
        $con->__set('timestamp',date("y-m-d h:m:s"));
        $con->__set('dep_code',$config->dep_code);
        $con->__set('dep_type',$config->dep_type);
        $con->__set('arr_code',$config->arr_code);
        $con->__set('arr_type',$config->arr_type);
        $con->__set('priority',$config->priority);
        $con->__set('status',$config->status);
        $con->__set('configuration_name',strtoupper($config->dep_code.$config->dep_type."_".$config->arr_code.$config->arr_type."_CN"));
        try{
            $con->save();
            $mark= new MarkupConfigs();
            $mark->__set('start_date',date("Y-m-d"));
            $mark->__set('end_date',date("Y-m-d"));
            $mark->__set('config_id',$config->id );
            $mark->__set('fare_type',"ALL");
            $mark->__set('airline_code',"ALL");
            $mark->__set('trip_type',"ALL");
            $mark->__set('ptc',"ALL");
            $mark->__set('cabin_class',"ALL");
            $mark->__set('sid',"ALL");
            $mark->__set('range',"ALL");
            $mark->__set('time_search',"ALL");
            $mark->__set('flight_type',"ALL");
            $rule_id=substr(uniqid("RL"),0,25);
            $mark->__set('rule_id',$rule_id);
            $mark_rule= new MarkupRules();
            $mark_rule->__set('idmarkup_rules',$rule_id);
            $mark_rule->__set('rule_name',"Default RULE for Config id ".$config->id);
            $mark_rule->__set('markup_type',"ADD");
            $mark_rule->__set('markup_value_type',"AMT");
            $mark_rule->__set('markup_apply_on',"TAX");
            $mark_rule->__set('value',28.00);
            $mark_rule->save();
            $mark->save();
            return $config->id; 
        }
        catch(Exception $e){
            echo $e->__toString();
            return "NOT GENERATED";
        }
 }
      function persist_request($alt)
      {
	 
          $req= new Requests();
          $req->__set('timestamp',date("y-m-d h:m:s"));
          $req->__set('username','NONE');
          $req->__set('config_id',$alt->config_id);
          $req->__set('options',$alt->opt);
          $req->__set('inc_arl',$alt->incl_arl);
          $req->__set('exc_arl',$alt->exc_arl);
          $req->__set('ptc',$alt->ptc);
          $req->__set('sid',$alt->sid);
          $req->__set('user_id','NONE');
          $req->__set('status',$alt->status);
          $req->__set('num_alt',$alt->num_alt);
          $req->__set('arl_opt',$alt->arl_opt);
          try
          {
          $req->save();
          return true;
          }
          catch(Exception $e )
          {
           return false;
          }
                   
      }
      function update_request($alt)
      {
		  $req=Doctrine::getTable('Requests')->find($alt->id);
          $timest= new Doctrine_Expression('NOW()');
          $req->__set('timestamp',$timest);
          $req->__set('username','NONE');
          $req->__set('config_id',$alt->config_id);
          $req->__set('options',$alt->opt);
          $req->__set('inc_arl',$alt->incl_arl);
          $req->__set('exc_arl',$alt->exc_arl);
          $req->__set('ptc',$alt->ptc);
          $req->__set('sid',$alt->sid);
          $req->__set('user_id','NONE');
          $req->__set('status',$alt->status);
          $req->__set('num_alt',$alt->num_alt);
          $req->__set('arl_opt',$alt->arl_opt);
          try
          {
          $req->save();
          return true;
          }
          catch(Exception $e )
          {
           return false;
          }
      }
  }
class markup_configs{
      var $start_date;
      var $end_date;
      var $config_id;
      var $fare_type;
      var $airline_code;
      var $trip_type;
      var $ptc;
      var $cabin_class;
      var $sid;
      var $range;
      var $time_search;
      var $flight_type;
      var $markup_type;
      var $markup_value_type;
      var $markup_apply_on;
      var $value;
      var $idMarkup_configs;
      function markup_configs($sd,$ed,$cid,$ft,$ac,$tt,$ptc,$cab,$sid,$ran,$ts,$flt,$mtyp,$mvltyp,$mappon,$val,$id=null)
      {
      $this->start_date=$sd;
      $this->end_date=$ed;
      $this->config_id=$cid;
      $this->fare_type=$ft;
      $this->airline_code=$ac;
      $this->trip_type=$tt;
      $this->ptc=$ptc;
      $this->cabin_class=$cab;
      $this->sid=$sid;
      $this->range=$ran;
      $this->time_search=$ts;
      $this->flight_type=$flt;
      $this->markup_type=$mtyp;
      $this->markup_value_type=$mvltyp;
      $this->markup_apply_on=$mappon;
      $this->value=$val;    
      $this->idMarkup_configs=$id;
      if($id==null)
      {
          $this->persist_mark_con();
      }
      else
      {
          $this->update_mark_con();
      }
      }
      
      function persist_mark_con()
      {
		  
            $mark= new MarkupConfigs();
            $mark->__set('start_date',$this->sd);
            $mark->__set('end_date',$this->ed);
            $mark->__set('config_id',$this->config_id );
            $mark->__set('fare_type',$this->fare_type);
            $mark->__set('airline_code',$this->airline_code);
            $mark->__set('trip_type',$this->trip_type);
            $mark->__set('ptc',$this->ptc);
            $mark->__set('cabin_class',$this->cabin_class);
            $mark->__set('sid',$this->sid);
            $mark->__set('range',$this->range);
            $mark->__set('time_search',$this->time_search);
            $mark->__set('flight_type',$this->flight_type);
            $rule_id=substr(uniqid("RL"),0,25);
            $mark->__set('rule_id',$rule_id);
            $mark_rule= new MarkupRules();
            $mark_rule->__set('idmarkup_rules',$rule_id);
            $mark_rule->__set('rule_name',"Default RULE for Config id ".$config->id);
            $mark_rule->__set('markup_type',$this->markup_type);
            $mark_rule->__set('markup_value_type',$this->markup_value_type);
            $mark_rule->__set('markup_apply_on',$this->markup_apply_on);
            $mark_rule->__set('value',$this->value);
            $mark_rule->save();
            $mark->save(); 
      }
      
function update_mark_con()
{
            $mark=Doctrine::getTable("MarkupConfigs")->find($this->idMarkup_configs);
            $mark_rule=Doctrine::getTable("MarkupRules")->find($mark->rule_id);
            $mark->__set('start_date',$this->sd);
            $mark->__set('end_date',$this->ed);
            $mark->__set('config_id',$this->config_id );
            $mark->__set('fare_type',$this->fare_type);
            $mark->__set('airline_code',$this->airline_code);
            $mark->__set('trip_type',$this->trip_type);
            $mark->__set('ptc',$this->ptc);
            $mark->__set('cabin_class',$this->cabin_class);
            $mark->__set('sid',$this->sid);
            $mark->__set('range',$this->range);
            $mark->__set('time_search',$this->time_search);
            $mark->__set('flight_type',$this->flight_type);
            $mark_rule->__set('markup_type',$this->markup_type);
            $mark_rule->__set('markup_value_type',$this->markup_value_type);
            $mark_rule->__set('markup_apply_on',$this->markup_apply_on);
            $mark_rule->__set('value',$this->value);
            $mark_rule->save();
            $mark->save(); 
      }   
  }
?>
